package com.umeox.watch.moto.chat.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.RemoteException
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.apiclient.model.GetFriendsAttrDTO
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.adapter.FriendSelectListAdapter
import com.umeox.watch.moto.chat.bean.DeviceFriendInfo
import com.umeox.watch.moto.chat.db.DBUtils.getContactList
import com.umeox.watch.moto.dataservice.DataProvider
import com.umeox.watch.moto.dataservice.proxy.PushApiClientProxy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CreateGroupActivity : ThemeAppCompatActivity(), FriendSelectListAdapter.OnItemClickListener {

    private lateinit var ivCreate: TextView
    private lateinit var contactList: RecyclerView
    private val adapter = FriendSelectListAdapter()
    private var isCreating = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_create_group)
        ivCreate = findViewById(R.id.iv_create)
        contactList = findViewById(R.id.contactList)
        contactList.adapter = adapter
        adapter.setOnItemClickListener(this)
        ivCreate.setOnClickListener {
            if (isCreating) {
                return@setOnClickListener
            }
            isCreating = true
            createGroup()
        }
        getData()
    }

    private fun createGroup() {
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                val selfName = DataProvider.getNickname()
                val defaultGroupName = "${selfName}'s Group"
                val friendIds = adapter.getSelectedFriendIds()
                createGroup(defaultGroupName, friendIds)
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                finish()
            }
        }
    }

    private fun getData() {
        lifecycleScope.launch(Dispatchers.IO) {
            val list = getDeviceFriendsList()
            withContext(Dispatchers.Main) {
                adapter.setData(list)
            }
        }
    }

    private fun getDeviceFriendsList(): List<DeviceFriendInfo> {
        //获取好友群聊配置列表
        val friendsAttrList = try {
            getFriendsAttr().records
        } catch (e: Exception) {
            listOf()
        }

        //获取设备好友列表
        val deviceFriendList = getContactList().filter {
            it.type == 2 && !it.isGroup
        }

        val deviceFriendInfoList = mutableListOf<DeviceFriendInfo>()
        //生成显示列表
        for (contactInfo in deviceFriendList) {
            var available = false
            for (deviceFriendAttrDTO in friendsAttrList) {
                if (deviceFriendAttrDTO.friendId == contactInfo.friendId) {
                    available = (deviceFriendAttrDTO.customGroup == 1)
                }
            }
            val deviceFriendInfo = DeviceFriendInfo(contactInfo, available, available)
            deviceFriendInfoList.add(deviceFriendInfo)
        }

        return deviceFriendInfoList
    }

    @Throws(RemoteException::class)
    fun getFriendsAttr(): GetFriendsAttrDTO {
        return PushApiClientProxy.fetchBinder().getFriendsAttr()
    }

    @Throws(RemoteException::class)
    fun createGroup(groupName: String, friendIds: List<String>) {
        PushApiClientProxy.fetchBinder().createGroup(groupName, friendIds)
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun onItemClick(position: Int, data: DeviceFriendInfo) {
        data.selected = !data.selected
        adapter.notifyItemChanged(position)
    }
}